package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
import org.apache.dubbo.config.annotation.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public User getByUsername(String username) {
        User user = baseMapper.getByUsername(username);
        return user;
    }

    @Override
    public List<String> findPermissionsByUserId(Long id) {
        List<Permission> permissionList = baseMapper.findPermissionsByUserId(id);
        List<String> permissions = new ArrayList<>();
        permissionList.forEach(c -> permissions.add(c.getCode()));
        return permissions;

    }
    @Override
    public List<User> findAll(){
//        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//        queryWrapper.
        List<User> userList = this.list();
        return userList;
    }

    @Override
    public boolean delete(String id) {
        this.removeById(Integer.parseInt(id));
        return true;
    }

//    @Override
//    public List<User> findUsers(QueryPageBean queryPageBean) {
//        //获取查询条件

    @Override
    public List<User> findUsers(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("user_name",queryString);
        List<User> userList = this.list(queryWrapper);
        return userList;
    }

    @Override
    public boolean updatePW(User user) {
        System.out.println(user.getUserName());
        System.out.println(user.getPwd());
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",user.getUserName());
        User one = this.getOne(queryWrapper);
        System.out.println(one.toString());

        one.setPwd(user.getPwd());
        this.saveOrUpdate(one);
        return true;

    }
}